import { Controller, Get, Post, Body, Param, Query, UseGuards, Request, HttpException, HttpStatus, Logger } from '@nestjs/common';
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
import { MessageService } from './message.service';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiBody } from '@nestjs/swagger';
// 正确导入 TLSSigAPIv2
// import import { API: TLSSigAPIv2 } from 'tls-sig-api-v2';

const {  Api:TLSSigAPIv2 } = require('tls-sig-api-v2');

@ApiTags('消息')
@Controller('message')
export class MessageController {
  private readonly logger = new Logger(MessageController.name);

  constructor(private readonly messageService: MessageService) {}


  @Get('list')
  @UseGuards(JwtAuthGuard)
  async getMessageList(
    @Request() req,
    @Query('conversationID') conversationID: string,
    @Query('count') count: number,
    @Query('lastMessageID') lastMessageID?: string
  ) {
    const userId = req.user.id;
    return this.messageService.getMessageList(userId, conversationID, count, lastMessageID);
  }
  
  @Post('send')
  @UseGuards(JwtAuthGuard)
  async sendMessage(
    @Request() req,
    @Body() messageData: {
      conversationID: string;
      content: string;
      type: string;
      payload?: any;
    }
  ) {
    const userId = req.user.id;
    return this.messageService.sendMessage(userId, messageData);
  }
  
  @Get('conversation/list')
  @UseGuards(JwtAuthGuard)
  async getConversationList(@Request() req) {
    const userId = req.user.id;
    return this.messageService.getConversationList(userId);
  }
  
  @Post('group/create')
  @UseGuards(JwtAuthGuard)
  async createGroup(
    @Request() req,
    @Body() groupData: {
      name: string;
      type: string;
      memberList: string[];
      avatar?: string;
      introduction?: string;
    }
  ) {
    const userId = req.user.id;
    return this.messageService.createGroup(userId, groupData);
  }
  
  @Post('group/join/:groupID')
  @UseGuards(JwtAuthGuard)
  async joinGroup(
    @Request() req,
    @Param('groupID') groupID: string
  ) {
    const userId = req.user.id;
    return this.messageService.joinGroup(userId, groupID);
  }
  
  @Post('group/quit/:groupID')
  @UseGuards(JwtAuthGuard)
  async quitGroup(
    @Request() req,
    @Param('groupID') groupID: string
  ) {
    const userId = req.user.id;
    return this.messageService.quitGroup(userId, groupID);
  }
  
  @Get('group/members/:groupID')
  @UseGuards(JwtAuthGuard)
  async getGroupMembers(
    @Param('groupID') groupID: string
  ) {
    return this.messageService.getGroupMembers(groupID);
  }
}




